home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CTOOLS10.ARJ / TSTOPT.C < prev    next >
C/C++ Source or Header  |  1991-12-31  |  6KB  |  198 lines

  1. /****************************************************************************
  2. *
  3. *                    Copyright (C) 1991 Kendall Bennett.
  4. *                            All rights reserved.
  5. *
  6. * Filename:        $RCSfile: CSTUB.C $
  7. * Version:        $Revision: 1.1 $
  8. *
  9. * Language:        ANSI C
  10. * Environment:    any
  11. *
  12. * Description:    A C program stub containing common interface code to getopt
  13. *                and other routines.
  14. *
  15. * $Id: CSTUB.C 1.1 91/08/16 11:19:56 ROOT_DOS Exp $
  16. *
  17. * Revision History:
  18. * -----------------
  19. *
  20. * $Log:    CSTUB.C $
  21. * Revision 1.1  91/08/16  11:19:56  ROOT_DOS
  22. * Initial revision
  23. ****************************************************************************/
  24.  
  25. #include "stdio.h"
  26. #include "stdlib.h"
  27. #include "string.h"
  28. #include "ctype.h"
  29. #include "dir.h"
  30. #include "getopt.h"                    /* Option parsing routines            */
  31.  
  32. /*------------------- Version number and test status ----------------------*/
  33.  
  34. #define VERSION            100            /* Version number * 100                */
  35. #define    TESTSTATUS        'b'            /* Test status of program            */
  36.  
  37. /*------------------------- Global variables ------------------------------*/
  38.  
  39. char    version[6];                    /* Version string (eg: 5.20b)        */
  40. char    nameofus[MAXFILE];            /* Name of program (no path)        */
  41. char    pathtous[MAXDIR];            /* Pathname to our program.            */
  42. char    *progname;                    /* Descriptive name of program        */
  43. char    *rcsid = "$Id: CSTUB.C 1.1 91/08/16 11:19:56 ROOT_DOS Exp $";
  44. char    *rev = "$Revision: 1.1 $";
  45.  
  46. /*-------------------------- Implementation -------------------------------*/
  47.  
  48. void init(char *argv0,char *prognam)
  49. /****************************************************************************
  50. *
  51. * Function:        init
  52. * Parameters:    argv0        - The argv[0] array entry.
  53. *                prognam        - Descriptive name of program.
  54. *
  55. * Description:    Init takes the pathname to our program as a parameter
  56. *                (found in argv[0]) and use this to set up three global
  57. *                variables:
  58. *
  59. *                pathtous    - Contains the pathname to our program
  60. *                nameofus    - Contains the name of the program (without the
  61. *                              .EXE extension)
  62. *                version        - Contains the version number of the program,
  63. *                              in the format 5.20b
  64. *
  65. *                Note that the version string is formatted to include an
  66. *                optional teststatus identifier. This can be any character
  67. *                such as 'a' for alpha test status, 'b' for beta test status
  68. *                or any other meaningful character. If it is a space, it is
  69. *                not included in the version string.
  70. *
  71. *                We also set up the global variable progname to point to
  72. *                the static string passed to init for all to use.
  73. *
  74. ****************************************************************************/
  75. {
  76.     char    *p,i;
  77.  
  78.     /* Obtain the path to our program from pathname */
  79.  
  80.     p = strrchr(argv0,'\\') + 1;
  81.     i = *p;
  82.     *p = '\0';
  83.     strcpy(pathtous,argv0);
  84.     *p = i;
  85.  
  86.     /* Obtain the name of our program from pathname */
  87.  
  88.     i = 0;
  89.     while (*p != '.')
  90.         nameofus[i++] = *p++;
  91.     nameofus[i] = '\0';
  92.  
  93.     /* Set up version string */
  94.  
  95.     i = 0;
  96.     p = &rev[11];
  97.     while (*p != ' ')
  98.         version[i++] = *p++;
  99.  
  100. #ifdef    TESTSTATUS
  101.     version[i++] = TESTSTATUS;
  102. #endif
  103.  
  104.     version[i] = '\0';
  105.  
  106.     progname = prognam;
  107. }
  108.  
  109. void banner(void)
  110. /****************************************************************************
  111. *
  112. * Function:     banner
  113. *
  114. * Description:  Prints a standard banner to the standard output
  115. *
  116. ****************************************************************************/
  117. {
  118.     printf("%s  Version %s - %s  Copyright (C) 1991 Kendall Bennett\n\n"
  119.         ,progname,version,__DATE__);
  120. }
  121.  
  122. int        myInt = 0;
  123. int        myHex = 0;
  124. int        myOct = 0;
  125. uint    myUint = 0;
  126. long    myLint = 0;
  127. long    myLHex = 0;
  128. long    myLOct = 0;
  129. ulong    myULint = 0;
  130. float    myFloat = 0.0;
  131. double    myDouble = 0.0;
  132. char    *myStr = "no string";
  133. bool    mySwitch = 0;
  134.  
  135. Option    optarr[] = {{'d',OPT_INTEGER,    &myInt,"An integer number"},
  136.                     {'x',OPT_HEX,        &myHex,"A hexidecimal number"},
  137.                     {'o',OPT_OCTAL,        &myOct,"An octal number"},
  138.                     {'u',OPT_UNSIGNED,    &myUint,"An unsigned integer"},
  139.                     {'D',OPT_LINTEGER,    &myLint,"A long integer"},
  140.                     {'X',OPT_LHEX,        &myLHex,"A long hexadecimal number"},
  141.                     {'O',OPT_LOCTAL,    &myLOct,"A long octal number"},
  142.                     {'U',OPT_LUNSIGNED,    &myULint,"An unsigned long integer"},
  143.                     {'f',OPT_FLOAT,        &myFloat,"A floating point number"},
  144.                     {'F',OPT_DOUBLE,    &myDouble,
  145.                         "A double precision floating point number"},
  146.                     {'s',OPT_STRING,    &myStr,"A string constant"},
  147.                     {'z',OPT_SWITCH,    &mySwitch,"An option switch"}};
  148.  
  149. #include <math.h>
  150.  
  151. void help(void)
  152. /****************************************************************************
  153. *
  154. * Function:     help
  155. *
  156. * Description:  Help provides usage information about our program if the
  157. *               options do make any sense.
  158. *
  159. ****************************************************************************/
  160. {
  161.     banner();
  162.     printf("Usage: %s\n\n",nameofus);
  163.     printf("Options are:\n");
  164.     print_desc(NUM_OPT(optarr),optarr);
  165.     exit(1);
  166. }
  167.  
  168. void main(int argc,char **argv)
  169. {
  170.     init(argv[0],"CStub");            /* Initialise a few globals            */
  171.  
  172.     myFloat = sqrt(myFloat);        /* Do this to link in math stuff    */
  173.  
  174.     switch (getargs(argc,argv,NUM_OPT(optarr),optarr,NULL)) {
  175.         case INVALID:
  176.             printf("Invalid option\a\n");
  177.             exit(1);
  178.             break;
  179.         case HELP:
  180.             help();
  181.         }
  182.  
  183.     printf("Values of command line options:\n\n");
  184.     printf("myInt     = %d\n",myInt);
  185.     printf("myHex     = %x\n",myHex);
  186.     printf("myOct     = %o\n",myOct);
  187.     printf("myUint    = %u\n",myUint);
  188.     printf("myLint    = %ld\n",myLint);
  189.     printf("myLHex    = %lx\n",myLHex);
  190.     printf("myLOct    = %lo\n",myLOct);
  191.     printf("myULint   = %lu\n",myULint);
  192.     printf("myFloat   = %f\n",myFloat);
  193.     printf("myDouble  = %lf\n",myDouble);
  194.     printf("myStr     = \"%s\"\n",myStr);
  195.     printf("mySwitch  = %s\n",mySwitch ? "true" : "false");
  196. }
  197.